package com.domino.client.view;

import com.domino.client.view.BoardGamePanel.State;
import com.domino.shared.Player;
import com.domino.shared.model.Color;
import com.domino.shared.session.SessionId;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.UIObject;

public class SessionInfoPanel {

	private Label statusLabel;
	private HTML messageLabel;
	private Label playerLabel;
	private Label opponentIdLabel;
	private Label sessionIdLabel;
	private Label pointsLabel;

	public SessionInfoPanel() {
		addSessionInfo();
	}
	
	private void addSessionInfo() {
		statusLabel = new Label("Initializing game...");
		messageLabel = new HTML();
		playerLabel = new Label("Welcome");
		opponentIdLabel = new Label("Opponent: -");
		sessionIdLabel = new Label("Session: -");
		pointsLabel = new Label("");
		RootPanel.get("pointsLabelContainer").add(pointsLabel);
		RootPanel.get("playerLabel").add(playerLabel);
		RootPanel.get("opponentLabel").add(opponentIdLabel);
		RootPanel.get("sessionLabel").add(sessionIdLabel);
		RootPanel.get("messageLabelContainer").add(messageLabel);
		RootPanel.get("statusLabel").add(statusLabel);
	}

	public void updatePoints(int colorCountBlack, int colorCountWhite) {
		pointsLabel.setText(Color.BLACK.name() + ": " + colorCountBlack +
				  " " + Color.WHITE.name() + ": " + colorCountWhite);
	}

	public NotificationPanel createNotificationPanel(UIObject target) {
		return new NotificationPanel(target);
	}

	public void updateStatus(String message) {
		statusLabel.setText(message);
	}

	public void updatePlayer(Player player, Color clientColor) {
		String msg = "";
		if (player!=null) {
			msg += "Welcome " + player.getNickname();
		}
		if (clientColor!=null) {
			msg+=", you play " + clientColor.name();
			statusLabel.setText("You're color is " + clientColor.name());
		}
		playerLabel.setText(msg);
	}

	public void updateOpponent(Player opponent) {
		opponentIdLabel.setText("Opponent: " + (opponent==null?"":opponent.getNickname()));
	}

	public void updateSessionId(SessionId sessionId) {
		sessionIdLabel.setText("Session: " + sessionId.getId());
	}

	public void updateStatInfo(State state) {
		String msg = null; 
		if (State.ClientActionTurn.equals(state)) {
			msg = "Your turn";
		} else if (State.OpponentActionTurn.equals(state)) {
			msg = "Wait for server...";
		} else if (State.WaitingToJoinAGame.equals(state)) {
			msg = "Wait for server...";
		} else if (State.WaitingForOpponentToJoin.equals(state)) {
			msg = "Wait for server...";
		} else if (State.Error.equals(state)) {
			msg = "An error occurred waiting for server...";
		} else if (State.GameFinished.equals(state)) {
			msg = "";
		} else {
			msg = "Unknown";
		}
		statusLabel.setText(msg);
		
	}

	public void joinNewSession(Player player) {
		updatePlayer(player, null);
		updateOpponent(null);
	}
	
	

}
